package com.atguigu.yygh.cmn.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.excel.EasyExcel;
import com.atguigu.yygh.cmn.listener.DictListener;
import com.atguigu.yygh.cmn.mapper.DictMapper;
import com.atguigu.yygh.cmn.service.DictService;
import com.atguigu.yygh.model.cmn.Dict;
import com.atguigu.yygh.vo.cmn.DictVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/** 
* @author 作者 Your-Name: 
* @version 创建时间：2021年3月25日 上午9:44:12 
* 类说明 
*/
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper,Dict> implements DictService {
//  ServiceImpl 类中 已实现 spring注入
	//@Autowired    protected M baseMapper;
	
//	@Autowired 
//	private DictMapper dictMapper;

	@Override
	@Cacheable(value = "dict",keyGenerator = "keyGenerator")
	public List<Dict> findChildData(Long id) {
		// TODO Auto-generated method stub
		QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("parent_id", id);//等价于 id = parent_id
		List<Dict> dlist = baseMapper.selectList(queryWrapper);
		//向List<Dict> 集合 每个list对象中设置 hasChildren
		for(Dict dict : dlist){
			Long dictId = dict.getId();
			boolean isChildren = this.isChildren(dictId);
			dict.setHasChildren(isChildren);
		}
		return dlist;
	}
	
	//判断 id 节点下是或否有子数据
	@Override
	public boolean isChildren(Long id) {
		// TODO Auto-generated method stub
		QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("parent_id", id);//等价于 id = parent_id
		int count = baseMapper.selectCount(queryWrapper);
		return count > 0;
	}

	//导出数据字典接口
	@Override
	public void exportDictExcel(HttpServletResponse response) {
		// TODO Auto-generated method stub
		//设置下载 相关内容
		response.setContentType("application/vnd.ms-excel");
		response.setCharacterEncoding("utf-8");
		//设置URLEncoder.encode 防止中文乱码  和 easyexcel没有关系
		String fileName = "dict";
		response.setHeader("Content-disposition", "attachment ; filename = " + fileName + ".xlsx");
		
		//查询数据库  数据字典信息
		List<Dict> list = baseMapper.selectList(null);
		
		List<DictVo> dlist = new ArrayList<DictVo>();
		for(Dict d : list){
			DictVo dv = new DictVo();
			//将 Dict的值 赋值给 DictVo
			BeanUtils.copyProperties(d, dv);
			dlist.add(dv);
		}
		try {
			//Dict  DictVo 内容不同 doWrite(list) 中 list 应为  DictVo对象
			EasyExcel.write(response.getOutputStream(),DictVo.class).sheet("dict")
			    .doWrite(dlist);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	//导入 数据字典 excel文件  //清空缓存内容
	@Override
	@CacheEvict(value = "dict",allEntries = true)
	public void importDictExcel(MultipartFile file) {
		// TODO Auto-generated method stub
		try {
			EasyExcel.read(file.getInputStream(),DictVo.class,new DictListener(baseMapper)).sheet().doRead();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public String getDictName(String dictCode, String value) {
		// TODO Auto-generated method stub
		// dictCode 为空  根据 value查询
		if(StringUtils.isEmpty(dictCode)){
			QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
			queryWrapper.eq("value", value);
			Dict dict = baseMapper.selectOne(queryWrapper);
			return dict.getName();
		}else{
		// 不为空  根据 dictCode + value 查询
			//根据dict_code 查询 dict对象 获取 id
			QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
			queryWrapper.eq("dict_code", dictCode);
			Dict codeDict = baseMapper.selectOne(queryWrapper);
			Long parent_id = codeDict.getId();
			//System.out.println(parent_id);
			//根据 parent_id value进行查询 
			Dict findDict = baseMapper.selectOne(new QueryWrapper<Dict>()
					.eq("parent_id", parent_id)
					.eq("value", value));
		    return findDict.getName();
		}
	}

	//根据dictCode 查询下级节点
	@Override
	public List<Dict> findByDictCode(String dictCode) {
		//根据dict_code 查询 dict对象 获取 id
		QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("dict_code", dictCode);
		Dict codeDict = baseMapper.selectOne(queryWrapper);
		Long parent_id = codeDict.getId();
		List<Dict> dictList = findChildData(parent_id);
		return dictList;
	}
	
	
	
}
